package 分治;


//使用二分查找
public class 二分搜索 {
    public static void search(int arr[], int n) {
        int begin = 0, end = arr.length - 1;
        int mid = end / 2;

        while (arr[mid] != n) {
            if (arr[begin] == n) {
                mid = begin;
                break;
            } else if (arr[end] == n) {
                mid = end;
                break;
            }
            mid = (end + begin) / 2;
            if (arr[mid] > n) {
                end = mid - 1;
            } else {
                begin = mid + 1;

            }
        }
        System.out.println(n + "为数组的第" + (mid + 1) + "位");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arr = new int[]{1, 3, 5, 6, 7};
        for (int i = 0; i <= arr.length - 1; i++)
            search(arr, arr[i]);

    }

}
